import time

from common.base import BaseMethod
import random
class AddGoodClass(BaseMethod):
    # 进入添加商品分类页面
    def go_to_good_class(self):  #方法中不能有大写
        # 点击商品按钮
        self.click("xpath","//a[text()='商品']")
        #点击分类列表
        self.click("xpath","//a[text()='分类列表']")
        #点击添加分类
        self.click("xpath","//span[text()='添加分类']")

    # 分类名称 --- 随机输入
    def input_ran_分类名称(self):
        # "goods100-100000"
        m = str(random.randint(100,100000))
        name = "goods" + m
        self.inputKeys("name","name",name)

    # 分类名称 --- 指定内容输入
    def input_分类名称(self,name):
        self.inputKeys("name", "name", name)


    # 上级分类  --- 随机选择
    def select_ran_上级分类(self):
        elems = self.get_elements("xpath","//select[@name='parent_id']/option")
        random.choice(elems).click()

    # 上级分类  --- 指定内容
    def select_上级分类(self,value):
        self.click("xpath","//select[@name='parent_id']/option[@value='{}']".format(value))

    # 商品模型  --- 随机选择
    def select_ran_商品模型(self):
        elems = self.get_elements("xpath","//select[@name='model']/option")
        random.choice(elems).click()

    # 商品模型  --- 指定内容
    def select_商品模型(self,value):
        self.click("xpath","//select[@name='model']/option[@value='{}']".format(value))

    # 排序  ---- 随机输入一个排序数据
    def input_ran_排序(self):
        m = random.randint(1,99)
        self.inputKeys("name","sort",m)

    # 排序  ---- 随机输入一个排序数据
    def input_排序(self,m):
        self.inputKeys("name", "sort", m)

    #一次性输入所有内容
    def input_all(self):
        self.input_ran_分类名称()
        self.select_ran_上级分类()
        self.select_ran_商品模型()
        self.input_ran_排序()

    # 点击确定按钮
    def click_确定(self):
        self.click("xpath","//span[text()='确 定']")

    # 通过url断言
    def assert_by_url(self,*value):
        hope = value[-1]
        time.sleep(3)
        self.assert_in(hope,self.get_url())
    #通过标题断言
    def assert_by_tile(self,*value):
        hope = value[-1]
        time.sleep(3)
        self.assert_in(hope,self.get_title())

    #通过元素的文本内容断言
    def assert_by_elem_text(self,*value):
        by, locate, hope = value
        actual = self.get_elem_text(by,locate)
        self.assert_in(hope,actual)


